#!/usr/bin/perl

$SEP=(`uname -a` =~ /CYGWIN/ ? ";" : ":");

$maxx=15;
$maxy=15;
$numenergy=0;

$numinfected=7;
$numantibody=7;
$numsusceptible=12;

$joinprob=0.6;

$infectedClass=$ARGV[0];
$antibodyClass=$ARGV[1];
$order=$ARGV[2];

@launched = ();
@invlaunched = ();
$numcells=0;

for($i=0; $i<$numenergy; $i++) {
	$x=int($maxx * rand());
	$y=int($maxy * rand());
	system("java -cp \"lib/java-getopt-1.0.13.jar${SEP}build\" assg.funnycells.cells.FunnyCell -x$x -y$y -t3 -ienergy$i -l0 &");
}

for($i=0; $i<$numsusceptible; $i++) {
	$x = 0;
	$y = 0;
	$done = 0;
	if($numcells == 0 || rand() >= $joinprob) {
		do {
			$x=int($maxx * rand());
			$y=int($maxy * rand());
		} while($invlaunched[$y*100 + $x] != 0);
		$done = 2;
	} else {
		$k = int(rand() * $numcells);
		$y = int($launched[$k] / 100);
		$x = $launched[$k] % 100;
		$startsearchx = 2 * int(rand() * 2) - 1;
		$startsearchy = 2 * int(rand() * 2) - 1;
		for($k=$startsearchx; $k != -2*$startsearchx; $k+=-$startsearchx) {
			for($j=$startsearchy; $j != -2*$startsearchy; $j+=-$startsearchy) {
				if($y+$j >= 0 && $y+$j < $maxy && $x+$k >= 0 && $x+$k < $maxx) {
					if($invlaunched[($y+$j)*100 + ($x+$k)] == 0) {
						$done=1;
						$invlaunched[$y*100 + $x] = $done;
						$y += $j;
						$x += $k;
						last;
					}	
				}
			}
			if($done == 1) {
				last;
			}
		}
	}
	if($done != 0) {
		$launched[$numcells]=$y*100 + $x;
		$invlaunched[$y*100 + $x] = $done;
		$numcells++;
	} else {
		$i--;
	}
}

$relocx = -1;
$relocy = -1;
$numincluster = 0;
for($i=$numsusceptible - 1; $i >= 0; $i--) {
	$y = int($launched[$i] / 100);
	$x = $launched[$i] % 100;
	$invl = $invlaunched[$y*100 + $x];
	$done = 0;
	if($invlaunched[$y*100 + $x] != 1) {
		if($relocx == -1) {
			$relocx = $x;
			$relocy = $y;
			$numincluster = 0;
			$done = 2;
		} else {
			for($rad=1; $rad <= 3; $rad++) {	
				for($k=-$rad; $k <= $rad; $k++) {
					for($j=-$rad; $j <= $rad; $j++) {
						if($relocy+$j >= 0 && $relocy+$j < $maxy && $relocx+$k >= 0 && $relocx+$k < $maxx) {
							if($invlaunched[($relocy+$j)*100 + ($relocx+$k)] == 0) {
								$done = 1;
								$y = $relocy + $j;
								$x = $relocx + $k;
								$invlaunched[$y*100 + $x] = $done;
								$numincluster++;
								if($numincluster > 3) {
									$numincluster = 0;
									$relocx = -1;
									$relocy = -1;
								}
								last;
							}	
						}
					}
					if($done == 1) {
						last;
					}
				}
				if($done == 1) {
					last;
				}
			}
		}
	} 
	system("java -cp \"lib/java-getopt-1.0.13.jar${SEP}build\" assg.funnycells.cells.FunnyCell -x$x -y$y -t0 -icell$i -l0 -r $infectedClass -g $antibodyClass &");	
}

@launched = ();

$type=$order + 1;
for($i=0; $i<$numinfected; ) {
	$x=int($maxx * rand());
	$y=int($maxy * rand());
	if($launched[$y*100 + $x] == 0) {
		system("java -cp \"lib/java-getopt-1.0.13.jar${SEP}build\" assg.funnycells.cells.FunnyCell -x$x -y$y -t$type -icell$numcells -l0 -r $infectedClass -g $antibodyClass &");
		$numcells++;
		$i++;
		$launched[$y*100 + $x]++;
	}
}

@launched = ();

$type=($order + 1) % 2 + 1;
for($i=0; $i<$numantibody; ) {
	$x=int($maxx * rand());
	$y=int($maxy * rand());
	if($launched[$y*100 + $x] == 0) {
		system("java -cp \"lib/java-getopt-1.0.13.jar${SEP}build\" assg.funnycells.cells.FunnyCell -x$x -y$y -t$type -icell$numcells -l0 -r $infectedClass -g $antibodyClass &");
		$numcells++;
		$i++;
		$launched[$y*100 + $x]++;
	}
}


